package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.dto.AccessLogExportData;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.commons.entity.UserAccessShopLog;
import club.xyes.zkh.ecommerce.dao.mapper.UserAccessShopLogMapper;
import club.xyes.zkh.ecommerce.service.UserAccessShopLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Create by 郭文梁 2019/4/18 0018 14:06
 * UserAccessShopLogServiceImpl
 * 用户浏览商铺相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@Service
@Slf4j
public class UserAccessShopLogServiceImpl extends BaseServiceImpl<UserAccessShopLog> implements UserAccessShopLogService {
    private UserAccessShopLogMapper mapper;

    @Autowired
    public UserAccessShopLogServiceImpl(UserAccessShopLogMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public void log(User user, Shop shop) {
        UserAccessShopLog accessLog = new UserAccessShopLog();
        accessLog.setShop(shop);
        accessLog.setUser(user);
        accessLog.setShopId(shop.getId());
        accessLog.setUserId(user.getId());
        UserAccessShopLog res = save(accessLog);
        log.debug("Save the access log {}", res);
    }

    @Override
    public PageInfo<UserAccessShopLog> findByUser(User user, Integer page, Integer rows) {
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> mapper.selectByUserIdFetchAll(user.getId()));
    }

    @Override
    public PageInfo<UserAccessShopLog> findByShopIdOrderByTime(Integer shopId, String order, Integer page, Integer rows) {
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> mapper.selectByShopIdOrderByTimeFetchAll(shopId, "", order));
    }

    @Override
    public PageInfo<UserAccessShopLog> findByShopIdAndOrder(Integer shopId, String username, String type, String order, int page, int rows) {
        String usernameQuery = "%" + username + "%";
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> {
            switch (type) {
                case "time":
                    mapper.selectByShopIdOrderByTimeFetchAll(shopId, usernameQuery, order);
                    break;
                case "amount":
                    mapper.selectByShopIdOrderByAmountFetchAll(shopId, usernameQuery, order);
                    break;
                case "frequency":
                    mapper.selectByShopIdOrderByFrequency(shopId, usernameQuery, order);
                    break;
                case "level":
                    mapper.selectByShopIdOrderByLevel(shopId, usernameQuery, order);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown order type:" + type);
            }
        });
    }

    @Override
    public List<UserAccessShopLog> findLikeUsername(Integer shopId, String username) {
        if ("".equals(username)) {
            return Collections.emptyList();
        }
        String queryString = "%" + username + "%";
        return mapper.selectByShopIdAndLikeUsername(shopId, queryString);
    }

    @Override
    public List<UserAccessShopLog> findByShopId(Integer shopId) {
        return mapper.selectByShopIdFetchAll(shopId);
    }

    @Override
    public List<AccessLogExportData> findExportDataByShopId(Integer shopId) {
        return mapper.selectExportDataByShopId(shopId);
    }
}
